const VType = {
    list: 'list',
    dict: 'dict',
    number: 'number',
    string: 'string',
    bool: 'bool',
    unkown: 'unkown'
};

function typeName(instance) {
    let tpName = VType.dict;
    switch (typeof instance) {
        case typeof '':
            tpName = VType.string;
            break;
        case typeof 1:
            tpName = VType.number;
            break;
        case typeof undefined:
            tpName = VType.unkown;
            break;
        case typeof 1.0:
            tpName = VType.number;
            break;
        case typeof true:
            tpName = VType.bool;
            break;
        case typeof {}:
            if (instance instanceof Array) {
                tpName = VType.list;
            }
            break;
        case typeof []:
            if (instance instanceof Array) {
                tpName = VType.list;
            }
            break;
    }
    return tpName;
}

export function getnode(obj, node, defaultValue) {
    var nodes = node;
    if (typeName(node) === VType.string) {
        nodes = node.split('.');
    }
    if (typeName(nodes) !== VType.list) {
        throw new Error('check getnode method if give a valid string or Array value for node');
    }

    if (typeof obj === typeof undefined) {
        return defaultValue;
    }
    if (nodes.length > 0) {
        let key = nodes[0];

        let value = obj[key];
        if (typeof value === typeof undefined) {
            return defaultValue;
        }
        // console.log('==',value,key)
        if (nodes.length === 1) {
            try {
                if (typeof defaultValue === typeof undefined) {
                    return value;
                } else {
                    if (typeName(defaultValue) === typeName(value)) {
                        return value;
                    } else {
                        return defaultValue;
                    }
                }
            } catch (error) {
                return defaultValue;
            }
        } else {
            nodes.shift();
            return getnode(value, nodes, defaultValue);
        }
    }
}

//demo